<?php
namespace Catalog\Data;

use Catalog\Entity\AttributeValuePrice;
use Catalog\Entity\Attribute;
/**
 *
 * @author Paolo Bertuzzo
 *        
 */
class AttributeDao extends AbstractDao
{

    /**
     * 
     * @param int $storeId
     * @param int $productId
     * @param string $type
     * @param string $language
     */
    public function getLocalizedValues($storeId, $productId, $type, $language)
    {
        $query = $this->entityManager->createQuery('select av, lavt, avp from Catalog\Entity\Attribute a, Catalog\Entity\ProductAttribute pa,' . 
                    ' Catalog\Entity\LocalizedAttributeValueText lavt, Catalog\Entity\AttributeValue av, Catalog\Entity\AttributeValuePrice avp' .
                    ' where pa.attributeId = a.attributeId and pa.productId = ?1 and a.type = ?2' .
                    ' and av.attributeId = a.attributeId and av.attributeValueId = lavt.attributeValueId and lavt.language = ?3 and lavt.type = \'name\'' .
                    ' and avp.attributeValueId = av.attributeValueId and avp.storeId = ?4');
        
        $query->setParameter(1, $productId);
        $query->setParameter(2, $type);
        $query->setParameter(3, $language);
        $query->setParameter(4, $storeId);
        $query->useResultCache(true);

        return $query->getResult();
    }
    
    /**
     * 
     * @param int $productId
     * @param string $type
     * @param string $language
     */
    public function getLocalizedAttribute($productId, $type, $language)
    {
        $query = $this->entityManager->createQuery('select lat from Catalog\Entity\Attribute a, Catalog\Entity\ProductAttribute pa, Catalog\Entity\LocalizedAttributeText lat' .
                        ' where pa.attributeId = a.attributeId and pa.productId = ?1 and a.type = ?2 and lat.attributeId = pa.attributeId and lat.language = ?3');
        
        $query->setParameter(1, $productId);
        $query->setParameter(2, $type);
        $query->setParameter(3, $language);
        $query->useResultCache(true);
        
        return $query->getResult();
    }

    /**
     * 
     * @param array $attributeIds
     * @param string $language
     */
    public function getLocalizedAttributesNames($attributeIds, $language)
    {
        $query = $this->entityManager->createQuery('select lat from Catalog\Entity\LocalizedAttributeText lat' .
        		' where lat.attributeId in (?1) and lat.type = \'name\' and lat.language = ?2');
        
        $query->setParameter(1, $attributeIds);
        $query->setParameter(2, $language);
        $query->useResultCache(true);
        
        return $query->getResult();
    }
    
    /**
     * 
     * @param array $attributeValueIds
     * @param string $language
     */
    public function getLocalizedAttributes($attributeValueIds, $language)
    {
        $query = $this->entityManager->createQuery('select lat, lavt from Catalog\Entity\LocalizedAttributeText lat, Catalog\Entity\AttributeValue av,' .
                ' Catalog\Entity\LocalizedAttributeValueText lavt where lavt.attributeValueId in (?1) and lavt.type = \'name\' and lavt.language = ?2' .
        		' and lat.language = ?3 and lat.type = \'name\' and av.attributeId = lat.attributeId and lavt.attributeValueId = av.attributeValueId');
        
        $query->setParameter(1, $attributeValueIds);
        $query->setParameter(2, $language);
        $query->setParameter(3, $language);
        $query->useResultCache(true);
        
        return $query->getResult();
    }
    
}

?>